﻿using AutoMapper;
using ZhiJiao.Models.Entities;
using ZhiJiao.Service.Dto;

namespace ZhiJiao.Service.Profiles
{
    /// <summary>
    /// 映射档案类
    /// </summary>
    public class ZJProfile:Profile
    {
        public ZJProfile()
        {
            CreateMap<Evaluation, EvaluationDto>()
                .ForMember(dest => dest.InsId, opt => opt.MapFrom(src => src.Instruction.Id))
                .ForMember(dest => dest.InstructionName, opt => opt.MapFrom(src => src.Instruction.Name))
                .ForMember(dest => dest.EvaluationPartIDs, opt => opt.Ignore())
                .ForMember(dest => dest.RuleIds, opt => opt.MapFrom(src=>src.Rules!=null?String.Join(",", src.Rules.Select(r => r.Id).ToList()) :""));
            // 与上面的互为反向映射
            CreateMap<EvaluationDto, Evaluation>()
                .ForMember(dest=>dest.Rules,opt=>opt.Ignore())
                .ForMember(dest=>dest.EvaluationParts,opt=>opt.Ignore());
                
            CreateMap<EvaluationPart, EvaluationPartDto>()
                  .ForMember(dest=>dest.evalId,opt=>opt.MapFrom(src=>src.Evaluation.Id))
                  .ForMember(dest=>dest.Evaluation,opt=>opt.Ignore())
                  .ForMember(dest=>dest.Questions,opt=>opt.MapFrom(src=>src.Questions.OrderBy(q=>q.Number).ToList()))
                  .ReverseMap();  //b   g  

            CreateMap<Option, OptionDto>()
                 .ForMember(dest=>dest.CreatedTime,opt=>opt.MapFrom(o=>o.CreatedTime.ToString("yyyy/MM/dd hh:mm:ss")))
                  .ReverseMap();

            CreateMap<Question, QuestionDto>()
                .ForMember(dest => dest.ResultIds, opt => opt.MapFrom(src => src.Result != null ? String.Join(",",src.Result.Select(o => o.Id).ToList()):null))
                .ForMember(dest=>dest.Options,opt=>opt.MapFrom(src=> src.Options!=null?src.Options.OrderBy(o=>o.Label):null))
                .ForMember(dest=>dest.OptionIds,opt=>opt.MapFrom(src=>src.Options!=null?String.Join(",",src.Options.Select(o=>o.Id).ToList()):""))
                .ForMember(dest=>dest.EvaluationPartId,opt=>opt.MapFrom(src=>src.EvaluationPart.Id))
                .ReverseMap()
                .ForMember(dest => dest.Result, opt => opt.Ignore());

            CreateMap<Rule, RuleDto>()
                .ForMember(dest => dest.InsId, opt => opt.MapFrom(src => src.Instruction.Id))
                .ReverseMap();

            CreateMap<Course, CourseDto>()
                .ReverseMap();

            CreateMap<CourseImage, CourseImageDto>()
                .ReverseMap();

            CreateMap<Instruction, InstructionDto>()
                .ReverseMap();
       
            CreateMap<ZJImage, ImageDto>().ReverseMap();
        }
    }
}
